package com.xtc.netty.io.nio.buffer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 *  zero copy 直接缓冲区
 * @author Administrator
 */
public class DirectBuffer {

    public static void main(String[] args) {

        try {
            String inFile = "C:\\Users\\Administrator\\Desktop\\百度网盘会员\\bufferDemo.txt";
            FileInputStream fis = new FileInputStream(inFile);
            FileChannel fisChannel = fis.getChannel();

            String outFile = "C:\\Users\\Administrator\\Desktop\\百度网盘会员\\bufferDemoCopy.txt";
            FileOutputStream fos = new FileOutputStream(outFile);
            FileChannel fosChannel = fos.getChannel();

            ByteBuffer buffer = ByteBuffer.allocateDirect(1024);

            while (true) {
                buffer.clear();

                int read = fisChannel.read(buffer);
                if (read == -1) {
                    break;
                }

                buffer.flip();
                fosChannel.write(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
